package me.zhengjie.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.pojo.sqlServer.PPStorage;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author krilo
 * @date 2024-09-19 8:31
 */
@Slf4j
@Component
public class EasyExcelUtil {

    public static List<PPStorage> readPPStorageFromExcel(InputStream inputStream, String originalFilename){

        List<PPStorage> res = new ArrayList<>();

        if (originalFilename == null) {
            return res;
        }

        try {
            EasyExcel.read(inputStream, PPStorage.class, new ReadListener<PPStorage>() {
                @Override
                public void invoke(PPStorage data, AnalysisContext context) {
                    // 每一行读取到的数据
                    res.add(data);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                    // 读取完成后的操作，通常不需要处理太多事情
                    log.info("Excel数据读取完成");
                }
            }).sheet().doRead();

            return res;
        } catch (Exception e) {
            log.error("文件读取错误", e);
            return new ArrayList<>();
        }

    }
}
